View Javadoc
1 /* 2 * Angkor Web Framework 3 * 4 * Distributable under LGPL license. 5 * See terms of license at gnu.org. 6 */ 7 8 package com.tirsen.angkor.widget; 9 10 import com.tirsen.angkor.RenderContext; 11 import com.tirsen.angkor.Component; 12 import com.tirsen.angkor.event.ChangeListener; 13 import com.tirsen.angkor.event.ChangeSource; 14 import com.tirsen.angkor.event.ChangeSourceHelper; 15 16 import java.beans.PropertyEditor; 17 import java.io.IOException; 18 19 /*** 20 * Takes input from user as text. 21 * 22 * <p> 23 * 24 * Has the following functionality: 25 * <li> fetches its value from a {@link ValueModel}. 26 * <li> updates its value to the {@link ValueModel}. 27 * <li> has a prompt as a {@link TextLabel}. 28 * <li> has a configurable size. 29 * 30 * 31 * <!-- $Id: TextInput.java,v 1.5 2002/10/13 19:59:23 tirsen Exp $ --> 32 * 33 * @author $Author: tirsen $ 34 * @version $Revision: 1.5 $ 35 * @see ValueModel 36 * @see TextLabel 37 */ 38 public class TextInput extends Component implements ChangeSource 39 { 40 private ValueModel model; 41 private TextLabel prompt; 42 private int size = -1; 43 private ChangeSourceHelper changeSourceHelper = new ChangeSourceHelper(this); 44 private String text = null; 45 private boolean password = false; 46 47 public TextInput(String id) 48 { 49 super(id); 50 } 51 52 public TextInput() 53 { 54 } 55 56 public TextInput(String prompt, ValueModel model) 57 { 58 setPrompt(prompt); 59 this.model = model; 60 } 61 62 public TextInput(ValueModel model) 63 { 64 this.model = model; 65 } 66 67 public void addChangeListener(ChangeListener listener) 68 { 69 changeSourceHelper.addChangeListener(listener); 70 } 71 72 public void removeChangeListener(ChangeListener listener) 73 { 74 changeSourceHelper.removeChangeListener(listener); 75 } 76 77 public void setSize(int size) 78 { 79 this.size = size; 80 } 81 82 /*** 83 * Set the prompt as a @{link TextLabel} widget. This enables more sofisticated display 84 * of a prompt than by using only a <code>String</code>. 85 * The prompt is displayed above the input-box. 86 */ 87 public void setPrompt(TextLabel prompt) 88 { 89 this.prompt = prompt; 90 } 91 92 /*** 93 * Sets the text of the prompt. The prompt is displayed above the input-box. 94 */ 95 public void setPrompt(String promptText) 96 { 97 prompt = new TextLabel(); 98 prompt.setLabel(promptText); 99 } 100 101 public void setVisible(boolean visible) 102 { 103 super.setVisible(visible); 104 prompt.setVisible(visible); 105 } 106 107 public void setModel(ValueModel model) 108 { 109 this.model = model; 110 } 111 112 /*** 113 * This widget is interested in parsing events. 114 * @return <code>true</code> 115 */ 116 public boolean isParsing() 117 { 118 return true; 119 } 120 121 /*** 122 * Retrieves the value in the parameter named by {@link #getId()} and updates 123 * the model with this value. The PropertyEditor specified by the model is used to 124 * parse the <code>String</code> into an <code>Object</code>. 125 */ 126 public void parse(RenderContext context) 127 { 128 super.parse(context); 129 if (isVisible()) 130 { 131 text = context.getRequestParameter(getId()); 132 if (text != null) 133 { 134 PropertyEditor editor = getModel().getPropertyEditor(); 135 editor.setAsText(text); 136 Object newValue = editor.getValue(); 137 getModel().setValue(newValue); 138 text = null; 139 changeSourceHelper.signalChangeEvent(context); 140 } 141 } 142 } 143 144 public void render(RenderContext context) throws IOException 145 { 146 if (isVisible()) 147 { 148 if (prompt != null) prompt.render(context); 149 150 context.emptyTag("BR"); 151 152 if (text == null) 153 { 154 Object value = getModel().getValue(); 155 PropertyEditor editor = getModel().getPropertyEditor(); 156 if (editor != null) 157 { 158 if (value != null) 159 { 160 editor.setValue(value); 161 text = editor.getAsText(); 162 } 163 else 164 { 165 text = ""; 166 } 167 } 168 else 169 text = "<error, no editor>"; 170 if (text == null) text = ""; 171 } 172 173 // TODO: recalculate width for different browsers here... 174 String type = password ? " type=\"password\"" : " type=\"text\""; 175 String sizeAttr = ""; 176 if (size != -1) sizeAttr = " size=\"" + size + "\""; 177 String name = " name=\"" + uniqueId(context) + "\""; 178 String value = " value=\"" + text + "\""; 179 String id = " id=\"" + uniqueId(context) + "\""; 180 181 context.emptyTag("INPUT" + type + id + sizeAttr + name + value); 182 183 if (getModel() == null || !getModel().isReadOnly()) context.registerParsingComponent(this); 184 } 185 } 186 187 public void setPassword(boolean password) 188 { 189 this.password = password; 190 } 191 192 public ValueModel getModel() 193 { 194 if (model == null) model = createDefaultModel(); 195 return model; 196 } 197 198 private BasicValueModel createDefaultModel() 199 { 200 return new BasicValueModel(); 201 } 202 }

This page was automatically generated by Maven